package p270jp.pxv.android.model;

import java.util.Date;
import java.util.List;
import p104de.greenrobot.dao.query.QueryBuilder;
import p104de.greenrobot.dao.query.WhereCondition;
import p210hh.PixivAccountManager;
import p270jp.pxv.android.legacy.constant.ContentType;
import p270jp.pxv.android.legacy.model.DaoManager;
import p270jp.pxv.android.legacy.model.LikedWork;
import p270jp.pxv.android.legacy.model.LikedWorkDao;
import p270jp.pxv.android.legacy.model.PixivWork;

/* renamed from: jp.pxv.android.model.LikedWorkDaoManager */
/* loaded from: classes2.dex */
public class LikedWorkDaoManager {
    private final DaoManager daoManager;
    private final PixivAccountManager pixivAccountManager;

    public LikedWorkDaoManager(DaoManager daoManager, PixivAccountManager bVar) {
        this.daoManager = daoManager;
        this.pixivAccountManager = bVar;
    }

    public void deleteAll() {
        this.daoManager.getWritableSession().getLikedWorkDao().deleteAll();
    }

    public void deleteByPixivWork(PixivWork pixivWork) {
        ContentType a = ContentType.Companion.m4608a(pixivWork);
        if (a != null) {
            LikedWorkDao likedWorkDao = this.daoManager.getWritableSession().getLikedWorkDao();
            QueryBuilder<LikedWork> queryBuilder = likedWorkDao.queryBuilder();
            queryBuilder.m7238j(likedWorkDao.queryBuilder().m7247a(LikedWorkDao.Properties.WorkId.m7284a(Long.valueOf(pixivWork.f19079id)), LikedWorkDao.Properties.ContentType.m7284a(a.toString()), new WhereCondition[0]), new WhereCondition[0]);
            LikedWork i = queryBuilder.m7239i();
            if (i != null) {
                likedWorkDao.deleteByKey(i.getId());
            }
        }
    }

    public List<LikedWork> findMangaList(int i) {
        QueryBuilder<LikedWork> queryBuilder = this.daoManager.getReadableSession().getLikedWorkDao().queryBuilder();
        queryBuilder.m7238j(LikedWorkDao.Properties.ContentType.m7284a(ContentType.MANGA.toString()), new WhereCondition[0]);
        queryBuilder.f12214g = Integer.valueOf(i);
        return queryBuilder.m7242f();
    }

    public void insertWithPixivWork(PixivWork pixivWork) {
        long j = this.pixivAccountManager.f16499e;
        ContentType a = ContentType.Companion.m4608a(pixivWork);
        int i = (0 > j ? 1 : (0 == j ? 0 : -1));
        if (i >= 0 || a == ContentType.MANGA) {
            LikedWorkDao likedWorkDao = this.daoManager.getWritableSession().getLikedWorkDao();
            QueryBuilder<LikedWork> queryBuilder = likedWorkDao.queryBuilder();
            queryBuilder.m7238j(likedWorkDao.queryBuilder().m7247a(LikedWorkDao.Properties.WorkId.m7284a(Long.valueOf(pixivWork.f19079id)), LikedWorkDao.Properties.ContentType.m7284a(a.toString()), new WhereCondition[0]), new WhereCondition[0]);
            LikedWork i2 = queryBuilder.m7239i();
            if (i2 != null) {
                i2.setLoggedInUserId(Long.valueOf(j));
                i2.setCreatedAt(new Date());
                likedWorkDao.update(i2);
                return;
            }
            Long valueOf = Long.valueOf(pixivWork.f19079id);
            Long valueOf2 = Long.valueOf(pixivWork.user.f19078id);
            if (i >= 0) {
                j = 0;
            }
            likedWorkDao.insert(new LikedWork(null, valueOf, valueOf2, Long.valueOf(j), a.toString(), new Date()));
            if (100 < likedWorkDao.count()) {
                QueryBuilder<LikedWork> queryBuilder2 = likedWorkDao.queryBuilder();
                queryBuilder2.m7240h(" ASC", LikedWorkDao.Properties.CreatedAt);
                queryBuilder2.m7243e(1);
                likedWorkDao.delete(queryBuilder2.m7242f().get(0));
            }
        }
    }
}
